module.exports = app => {
  const express = require("express")
  const auth = require("../../middleware/auth")

  const router = express.Router({
    mergeParams: true
  })
  //学生端，获取个人信息
  router.get('/info', async (req, res) => {
    let queryOptions = {}
    queryOptions.populate = [{
      path: 'class',
      select: ['name', 'nid']
    }, {
      path: 'specialty',
      select: ['name', 'nid'],
      populate: {
        path: 'superior',
        select: ['name', 'nid'],
        populate: {
          path: 'superior',
          select: ['name', "nid"]
        }
      }
    }]
    const Student = require("../../models/Student")
    let model = await Student.findById(req.user.bindId).setOptions(queryOptions)
    res.send(model)
  })
  //学生端修改个人信息
  router.put('/info', async (req, res) => {
    const Student = require("../../models/Student")
    try {
      let model = await Student.findByIdAndUpdate(req.user.bindId, req.body)
      if (model) {
        res.send({
          success: true,
          message: '修改成功！'
        })
      }
    } catch (error) {
      res.send({
        success: false,
        message: '修改失败！'
      })
    }
  })
  //学生端，获取用户信息
  router.get('/userInfo', async (req, res) => {
    const User = require("../../models/User")
    let model = await User.findById(req.user._id, {
      "username": 1
    })
    res.send(model)
  })
  //学生端修改密码
  router.put("/password", async (req, res) => {
    const User = require('../../models/User')
    try {
      let model = await User.findByIdAndUpdate(req.user._id, req.body)
      res.send({
        success: true,
        message: '修改成功！'
      })
    } catch (error) {
      res.send({
        success: false,
        message: '修改失败！'
      })
    }
  })
  //学生端获取课表
  router.get("/schedule", async (req, res) => {
    const Student = require("../../models/Student")
    const Course = require("../../models/Course")
    let queryOptions = {}
    queryOptions.populate = [{
      path: 'teacher',
      select: ['name']
    }]

    let studentId = req.user.bindId;
    let studentRes = await Student.findById(studentId)
    let classId = studentRes.class;
    const {
      xn,
      xq
    } = req.query;
    let courseList = await Course.find({
      class: classId,
      year: xn,
      xq
    }).setOptions(queryOptions)
    res.send(courseList)

  })
  //学生端获取学年成绩
  router.get('/getXnGrade', async (req, res) => {
    const Grade = require("../../models/Grade")
    let year = req.query.xn,
      studentId = req.user.bindId,
      coursenature = req.query.coursenature;

    let gradeList = await Grade.find({
      student: studentId
    }).populate('course');

    let showGradeList = []
    if (coursenature != "''" && coursenature) {
      showGradeList = gradeList.filter(value => {
        return value.course.year == year && value.isEdit && value.course.coursenature == coursenature;
      })
    } else {
      showGradeList = gradeList.filter(value => {
        return value.course.year == year && value.isEdit;
      })
    }

    res.send(showGradeList)
  })
  //学生端获取学期成绩
  router.get('/getXqGrade', async (req, res) => {
    const Grade = require("../../models/Grade")
    let year = req.query.xn,
      studentId = req.user.bindId,
      coursenature = req.query.coursenature,
      xq = req.query.xq;

    let gradeList = await Grade.find({
      student: studentId
    }).populate('course');

    let showGradeList = []
    if (coursenature != "''" && coursenature) {
      showGradeList = gradeList.filter(value => {
        return value.course.xq == xq && value.course.year == year && value.isEdit && value.course.coursenature == coursenature;
      })
    } else {
      showGradeList = gradeList.filter(value => {
        return value.course.xq == xq && value.course.year == year && value.isEdit;
      })
    }

    res.send(showGradeList)
  })
  //学生端获取历年成绩
  router.get('/getHistoryGrade', async (req, res) => {
    const Grade = require("../../models/Grade")
    let studentId = req.user.bindId,
      coursenature = req.query.coursenature;
    let gradeList = await Grade.find({
      student: studentId
    }).populate("course")

    let showGradeList = []
    if (coursenature != "''" && coursenature) {
      showGradeList = gradeList.filter(value => {
        return value.course.coursenature = coursenature && value.isEdit
      })
    } else {
      showGradeList = gradeList.filter(value => {
        return value.isEdit
      })
    }

    res.send(showGradeList)
  })
  //学生端获取成绩统计表里的信息
  router.get('/getCountGradeList', async (req, res) => {
    const Grade = require('../../models/Grade');
    const natrueList = ["必修课", "选修课"]
    let student = req.user.bindId;
    const needXf = [91.50, 20.50];
    const getXf = {}
    for (let z = 0; z < natrueList.length; z++) {
      getXf[natrueList[z]] = 0;
    }


    let gradeList = await Grade.find({
      student,
      isEdit: true
    }).populate('course')

    for (let i = 0; i < natrueList.length; i++) {
      gradeList.forEach(value => {
        if (value.course.coursenature == natrueList[i]) {
          getXf[natrueList[i]] += value.xf;
        }
      })
    }

    let getXfList = []
    for (i in getXf) {
      getXfList.push(getXf[i])
    }

    let sendList = []
    natrueList.forEach((value, index) => {
      let temp = needXf[index] - getXfList[index]
      sendList.push({
        coursenatrue: natrueList[index],
        xfNeed: needXf[index].toFixed(2),
        getXf: getXfList[index].toFixed(2),
        needXf: temp.toFixed(2)
      })
    })

    res.send(sendList)
  })
  //获取成绩统计上下两个的信息
  router.get('/getCountGradeTopAndFoot', async (req, res) => {
    let student = req.user.bindId;
    const Grade = require('../../models/Grade')
    let gradeList = await Grade.find({
      student,
      isEdit: true
    }).populate('course')
    let needXf = 0, //所选学分
      getXf = 0, //获取学分
      xfJdCount = 0, //绩点和学分综合
      averageGpa = 0, //平均绩点
      countGpa = 0; //所有绩点
    gradeList.forEach(value => {
      needXf = needXf + value.course.zxf
      getXf = getXf + value.xf
      xfJdCount = xfJdCount + value.xf + value.gpa;
      countGpa = countGpa + value.gpa
      // needXf=(needXf+value.course.zxf).toFixed(2)
    })
    averageGpa = (countGpa / gradeList.length).toFixed(2)

    res.send({
      choseXf: needXf.toFixed(2),
      getXf: getXf.toFixed(2),
      averageGpa,
      xfJdCount: xfJdCount.toFixed(2),
    })
  })
  app.use('/admin/api/studentPageAPI', auth(), router)
}